package com.liuzu.esys.controller;

import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.common.R;
import com.liuzu.esys.pojo.Message;
import com.liuzu.esys.service.MessageService;
import com.liuzu.esys.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;
    //分页和模糊查询的接口
    @RequestMapping("/messageList")
    public LayuiPageVo<Message> messagePageData(@RequestParam Map<String,Object> params){

        return messageService.messagePageData(params);
    }
    //添加合作商信息
    @GetMapping("/addMessage")
    public R addUser(Message message){

       // System.out.println(message);
        int result = messageService.addMessage(message);
        if (result == 1) {
            return R.ok();
        }else {
            return R.error();
        }

    }
    //营业执照照片
    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        //避免预检请求报错  这里有个 if  判断
        if (file != null) {
            //得到文件名
            String originalFilename = file.getOriginalFilename();
            // 将文件转为输入流
            InputStream inputStream = file.getInputStream();

            String url = OssUtil.uploadFile(inputStream, originalFilename);

            //把文件 url 写给前端

            return R.ok().data("url", url);
        }
        return R.ok();
    }
    //修改合作商信息
    @RequestMapping("/updateMessage")
    public R updateMessage(Message message){
        System.out.println(message);
        int result = messageService.updateMessage(message);
        if (result == 1) {
            return R.ok();
        }else {
            return R.error();
        }

    }
    //删除合作商信息
    @RequestMapping("/deleteMessage")
    public R deleteMessage(Integer id){
        int result = messageService.deleteMessage(id);
        if (result == 1) {
            return R.ok();
        }else {
            return R.error();
        }

    }
    //批量删除合作商信息
    @GetMapping("/deleteMessageByIds")
    public R deleteMessageByIds(String ids){
        int result = messageService.deleteMessageByIds(ids);
        if (result>0){
            return R.ok();

        }else {
            return R.error();
        }
    }

}
